package boun.swe.bob.service;


import boun.swe.bob.entities.Group;
import boun.swe.bob.entities.User;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.apache.commons.lang.xwork.StringEscapeUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ememory
 * Date: 27.Ara.2010
 * Time: 18:01:12
 * To change this template use File | Settings | File Templates.
 */

public class GroupController extends BaseController {
    @RequestMapping(method = RequestMethod.GET)
        public ModelAndView showPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
            Map<String, Object> myModel = Maps.newHashMap();

            String action = httpServletRequest.getParameter("action");
            if(!Strings.isNullOrEmpty(action)) {
                int groupId = Integer.parseInt(httpServletRequest.getParameter("group_id"));
                if("remove".equals(action)) {
                    groupDao.LeaveGroup(groupId, getLoggedInUser(httpServletRequest).getUserId());
                } else if("add".equals(action)) {
                    groupDao.JoinGroup(groupId, getLoggedInUser(httpServletRequest).getUserId());
                }
                try {
                    httpServletResponse.sendRedirect("/bob-web/service/group.htm?group_id=" + groupId);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
            myModel.put("himself",true);
            Group grp=groupDao.findGroupByGroupId(Integer.parseInt(httpServletRequest.getParameter("group_id")));
            myModel.put("group",grp);
            myModel.put("topMembers",grp.getTopMembersList());
            myModel.put("topFeeds",grp.getTopFeedsList());
            myModel.put("isJoined",groupDao.IsUserGroup(Integer.parseInt(httpServletRequest.getParameter("group_id")),getLoggedInUser(httpServletRequest).getUserId()));
            myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));

            return new ModelAndView("group", "model", myModel);
        }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView handlePost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Map<String, Object> myModel = Maps.newHashMap();

        String feedCountStr = httpServletRequest.getParameter("feedCount");
        int feedCount = 5;
        if(null != feedCountStr) {
            feedCount = Integer.parseInt(feedCountStr) + 5;
        }

        String tweet = httpServletRequest.getParameter("tweet");
        try {
            tweet = StringEscapeUtils.unescapeHtml(tweet);
        }
        catch (Exception exp) {
        }


        User user = getLoggedInUser(httpServletRequest);

        if (!Strings.isNullOrEmpty(tweet) && !tweet.equals("\r\n\r\n                ")) {
            compositeDao.addGroupFeed(getLoggedInUser(httpServletRequest).getUserId(),Integer.parseInt(httpServletRequest.getParameter("group_id")),tweet);
        }
                    

        Group grp=groupDao.findGroupByGroupId(Integer.parseInt(httpServletRequest.getParameter("group_id")));
        myModel.put("group",grp);
        myModel.put("topMembers",grp.getTopMembersList());
        myModel.put("topFeeds",grp.getTopFeedsList());
        myModel.put("himself", true);
        myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
                     
        return new ModelAndView("group", "model", myModel);

    }
}